package com.yimixian.app.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.view.View;

public class JiShiSongProgressBar extends View {

	private Context context;

	public JiShiSongProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context);
	}

	public JiShiSongProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public JiShiSongProgressBar(Context context) {
		super(context);
		initView(context);
	}

	private float width, height;
	private float sideCircleRadius;
	private Bitmap baseBitmap, progressBitmap, compositeBitmap;
	private Canvas baseCanvas, progressCanvas, compositeCavans;
	private float percent = 0.33f;
	private float progressWidth;
	private float movedVariable = 0;
	private int colorOne = 0xffff5a1e;
	private int colorTwo = 0xfff24101;
	private int backColor = Color.LTGRAY;
	//	private int colorTwo = 0xB3f24101;
	private int leftSeconds = -1; //初始剩余秒数
	private int passSeconds = -1; //已经过去的秒数
	private boolean isSaveNewStartTime = true;
	private long startTime;
	private boolean isComplete = false;//是否全部加载 percent = 1
	private long startSysTime;
	private long invalidateInterval = 30; //30毫秒 刷新一次内部阴影位置

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
		height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
		sideCircleRadius = height / 2;
		progressWidth = width - sideCircleRadius * 2;
		setMeasuredDimension((int) width, (int) height);
	}

	private void initView(Context context){
		this.context = context;
	}

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);

		if(baseBitmap == null){
			baseBitmap = Bitmap.createBitmap((int) width, (int) height, Config.ARGB_8888);
			baseCanvas = new Canvas(baseBitmap);
			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setColor(backColor);
			baseCanvas.drawColor(Color.TRANSPARENT);
			baseCanvas.drawCircle(sideCircleRadius, sideCircleRadius, sideCircleRadius, paint);
			baseCanvas.drawCircle(width - sideCircleRadius, sideCircleRadius, sideCircleRadius, paint);
			baseCanvas.drawRect(sideCircleRadius, 0, width - sideCircleRadius, height, paint);
		}

		canvas.drawBitmap(baseBitmap, 0, 0, null);

		if(isSaveNewStartTime){
			startTime = System.currentTimeMillis();
			isSaveNewStartTime = false;
		}

		float passTime = Float.valueOf(System.currentTimeMillis() - startTime);

		if(passTime > leftSeconds * 1000)
			passTime = leftSeconds * 1000;
		percent = (passSeconds * 1000 + passTime) / ((passSeconds + leftSeconds) * 1000);

		if(onProgressMoveListener != null && !isComplete){
			onProgressMoveListener.onMove(percent);
		}

		if(percent == 1){
			isComplete = true;
		}else{
			isComplete = false;
		}

		if(compositeBitmap == null){
			compositeBitmap = Bitmap.createBitmap((int) width, (int) height, Config.ARGB_8888);
			compositeCavans = new Canvas(compositeBitmap);
			compositeCavans.drawBitmap(baseBitmap, 0, 0, null);
		}

		if(progressBitmap == null){
			progressBitmap = Bitmap.createBitmap((int) width, (int) height, Config.ARGB_8888);
			progressCanvas = new Canvas(progressBitmap);
		}

		Paint progressPaint = new Paint();
		progressPaint.setAntiAlias(true);
		LinearGradient lg = new LinearGradient(0 + movedVariable, 30, 30 + movedVariable, 0,
				new int[]{colorOne, colorOne, colorTwo, colorTwo, colorOne, colorOne, colorTwo, colorTwo, colorOne, colorOne, colorTwo, colorTwo, colorOne, colorOne, colorTwo, colorTwo},
				new float[]{0, 0.125f, 0.125f, 0.25f, 0.25f, 0.375f, 0.375f, 0.5f, 0.5f, 0.625f, 0.625f, 0.75f, 0.75f, 0.875f, 0.875f, 1f}, TileMode.REPEAT);
		progressPaint.setShader(lg);

		progressCanvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
		progressCanvas.drawRect(0, 0, percent * width, height, progressPaint);
		Paint compositePaint = new Paint();
		compositePaint.setAntiAlias(true);
		compositePaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		compositeCavans.drawColor(Color.TRANSPARENT, Mode.CLEAR);
		compositeCavans.drawBitmap(baseBitmap, 0, 0, null);
		compositeCavans.drawBitmap(progressBitmap, 0, 0, compositePaint);

		canvas.drawBitmap(compositeBitmap, 0, 0, null);

		long curTime = System.currentTimeMillis();
		if(startSysTime == 0 || curTime - startSysTime > invalidateInterval){
			startSysTime = curTime;
			movedVariable += 2;
		}

		postInvalidate();
	}

	public void setPercent(float percent) {
		this.percent = percent;
	}

	/**
	 * 开始进度条
	 * @param leftSeconds 剩余倒计时秒数
	 */
	public void startProgress(int leftSeconds){
		startProgress(leftSeconds, 0);
	}

	/**
	 *  开始进度条
	 * @param leftSeconds 剩余倒计时秒数
	 * @param passSeconds 已经过去的时间
	 */
	public void startProgress(int leftSeconds, int passSeconds){
		this.leftSeconds = leftSeconds;
		this.passSeconds = passSeconds;
		isSaveNewStartTime = true;
		startSysTime = System.currentTimeMillis();
		postInvalidate();
	}

	public void setBackColor(int backColor) {
		this.backColor = backColor;
		if(baseBitmap != null){
			baseBitmap.recycle();
			baseBitmap = null;
		}
	}

	public interface OnProgressMove{
		/**
		 * 当前移动百分比0-1
		 * @param percent
		 */
		public void onMove(float percent);
	}

	private OnProgressMove onProgressMoveListener;

	public void setOnProgressMoveListener(OnProgressMove onProgressMoveListener) {
		this.onProgressMoveListener = onProgressMoveListener;
	}

	public boolean isInit(){
		return leftSeconds == - 1 || passSeconds == -1;
	}
}
